package TUILayer;
import java.util.Scanner;
import CtrLayer.*;
import ModelLayer.*;

/**
 * PersonUI is the class responsible for handling the TUI of the person UI.
 * 
 * @author Gruppe 3 
 * @version 21-12-2011
 */

public class PersonUI
{
    private PersonCtr personCtr; //SaleUI must know the appropriate controller.

    /**
     * Constructs an object of the class PersonUI and creates and initialize a personCtr.
     */

    public PersonUI()
    {
        personCtr = new PersonCtr();

    }

    /**
     * Starts the person menu. 
     */

    public void start()
    {
        PersonMenu();
    }

    /**
     * Prints Main Menu to terminalwindow and accepts input (choices).
     */
    private void PersonMenu()
    {
        boolean exit = false;
        while(!exit)
        {
            int choice = writePersonMenu(); // show menu and return user's choice
            if (choice == 1)
            {
                createEmployee();
            }
            else if (choice == 3)
            {
                exit = true;
            }
            else if(choice==2)
            {
                createNewCustomer();
            }
        }
    }

    /**
     * Writes the item menu.
     */

    private int writePersonMenu()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        int choice = 0;
        while(!ok)
        {
            // makes an object keyboard to have input from the console
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Person Menu ***");
            System.out.println(" (1) Opret ny sælger ");
            System.out.println(" (2) Opret ny kunde ");
            System.out.println(" (3) Gå tilbage til hoved menu ");
            System.out.print("\n Tast dit valg: ");
            try
            {
                choice = keyboard.nextInt();
                ok = true;
            }
            catch (Exception e)
            {
                printCatch();
                returnToMenu();
            }
        }//end while
        return choice;
    }

    /**
     * Creates an employee.
     * 
     */

    private void createEmployee()
    {
        try
        {
            boolean ok=false;
            String name = inputName();
            String phoneNumber = inputPhoneNumber();
            String address = inputAddress();
            String city = inputCity();
            String zipCode = inputZipCode();
            String userName = inputUserName();
            double wage = inputWage();
            String employeeRole = inputEmployeeRole();
            while(!ok)
            {
                String password = inputPassword();
                System.out.println("");
                System.out.println("Gentag adgangskoden: ");
                String checkPassword = inputPassword();
                if(password.equals(checkPassword))
                {
                    personCtr.createEmployee(name, phoneNumber, address, city, zipCode, userName, password, wage, employeeRole);
                    ok=true;

                }
                else
                {
                    System.out.println("");
                    System.out.println("Adgangskoden passer ikke med den du først indtastede! - Prøv igen!");
                }
            }
        }
        catch(Exception e)
        {
            System.out.println("");
            System.out.println("Fejl opstod.");
            returnToMenu();
        }
    }

    /**
     * Creates a new customer.
     */

    private void createNewCustomer()
    {
        try
        {
            boolean ok=false;
            String name = inputName();
            String phoneNumber = inputPhoneNumber();
            String address = inputAddress();
            String city = inputCity();
            String zipCode = inputZipCode();
            int customerGroup = inputCustomerGroup();

            personCtr.createCustomer(name, phoneNumber, address, city, zipCode, customerGroup);
        }
        catch(Exception e)
        {
            System.out.println("");
            System.out.println("Fejl opstod.");
            returnToMenu();
        }

    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return name, the name of the customer or employee. 
     */

    private String inputName()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast navn: ");
        String name = keyboard.nextLine();
        return name;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return phoneNumber, the phone number of the customer or employee. 
     */

    private String inputPhoneNumber()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast telefonnummer: ");
        String phoneNumber = keyboard.nextLine();
        return phoneNumber;

    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return address, the address of the customer or employee. 
     */

    private String inputAddress()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast adresse: ");
        String address = keyboard.nextLine();
        return address;

    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return city, the city of the customer or employee. 
     */

    private String inputCity()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast by: ");
        String city = keyboard.nextLine();
        return city;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return zipCode, the zip code of the customer or employee. 
     */

    private String inputZipCode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast postnummer: ");
        String zipCode = keyboard.nextLine();
        return zipCode;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return userName, the user name of the customer or employee. 
     */

    private String inputUserName()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast brugernavn: ");
        String userName = keyboard.nextLine();
        return userName;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return wage, the wage of the employee. 
     */

    private double inputWage()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast løn: ");
        double wage = keyboard.nextDouble();
        return wage;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return employeeRole, the employee role of the employee. 
     */

    private String inputEmployeeRole()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast stilling: ");
        String employeeRole = keyboard.nextLine();
        return employeeRole;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return password, the password of the employee. 
     */

    private String inputPassword()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast adgangskode: ");
        String password = keyboard.nextLine();
        return password;
    }

    /**
     * Writes a command in the terminal to answer.
     * 
     * @return customerGroup, the customer group of the customer.
     */

    private int inputCustomerGroup()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n Indtast kundegruppe: ");
        int customerGroup = keyboard.nextInt();
        return customerGroup;
    }

    /**
     * Prints out acommand that you should type in a number.
     */

    private void printCatch()
    {
        System.out.println("\n Du skal indtaste et tal - prøv igen!");
    }

    /**
     * Writes that you should type retur to return to the menu.
     * 
     * @return input, your answer to the print. 
     */

    private String returnToMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("\n - Tryk på vilkårlig tast for at komme tilbage til menu.");
        String input = keyboard.nextLine();
        return input;
    }

}
